package com.hy.mydesktop.client.mvc.panel.tab;

import java.util.HashMap;
import java.util.Map;

import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Component;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.client.mvc.base.BaseController;
import com.hy.mydesktop.client.mvc.util.ControllerUtil;
import com.hy.mydesktop.client.mvc.window.WindowDisplay;

public class TabPanelControler extends BaseController  {
	
	private TabPanelView tabPanelView;
	
	public TabPanelControler() {
	   
	  }

	@Override
	public void handleEvent(AppEvent event) {
		
		Map<String, Object> map = (Map<String, Object>)event.getData();
		EventStructureMetaModel eventStructureMetaModel =null;
		if (map!=null) {
			eventStructureMetaModel =(EventStructureMetaModel)map.get("eventstructuremetamodel");
		}
		//EventStructureMetaModel eventStructureMetaModel =(EventStructureMetaModel)((Map<String, Object>)event.getData()).get("eventstructuremetamodel");
		//0007
		if (AppEvents.DESKTOP_WINDOWAPPLICATION_INIT==event.getType()) {
			//if (eventStructureMetaModel==null||this.getId()==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId().equals(this.getId())) {
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
	
			//转发到视图View
				forwardToView(tabPanelView, event);
				/**
				 * 获取Controller对应的View的组件Component
				 * 把Component，作为事件的一部分，向下传递
				 * map.put("parent", windowView.getWindow());
				 * 此方法错误
				 */
					Map<BaseController, Component> subMap = (Map<BaseController, Component>)map.get("controllerandcomponent");;
						subMap.put(this,  tabPanelView.getTabPanel());
				/**
				 * 把Controller和,与之对应的Component放入Map（为了方便查找Component的父元素）
				 */
				map.put("controllerandcomponent", subMap);
				/**
				 * 如何知道事件已经转发完毕
				 * 
				 * 方法1 ： 计数器		
				 */
				
				if (this.getChildren()!=null&&this.getChildren().size()>0) {
					/**
					 * 将事件转发给子节点，及子控制器
					 * 注:当前控制器，含有子节点的时候，必不可少
					 * 
					 * 采用先序遍历的方式---树
					 */
					this.forwardToChild(new AppEvent(AppEvents.DESKTOP_WINDOWAPPLICATION_INIT,map));
				} /*else {
					
					*//**
					 * 如果当前控制器，有子节点，则向下转发事件，
					 * 否则渲染组件，即渲染顶级元素
					 * 如何渲染顶级元素，即Window ？
					 *//*
					forwardToView(windowView,  AppEvents.DESKTOP_APPLICATION_RENDER,event.getData());
				}*/
				/*int count =( (Integer)map.get("count")-1);
				if (count<=0) {
					Dispatcher.forwardEvent(AppEvents.DESKTOP_APPLICATION_RENDER,map);
				}else {
					map.put("count", count);
				}*/
				ControllerUtil.isRenderer(map);

			}		
		}else {
			this.forwardToChild(event);
		}
	}
	
	public void initialize() {
		tabPanelView = new TabPanelView(this);
	  }
	
	protected void onError(AppEvent ae) {
	    System.out.println("error: " + ae.<Object>getData());
	  }
}
